package com.android.hedgehog;

import java.util.List;

import android.graphics.Color;
import android.util.Log;

import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Input.TouchEvent;
import com.android.hedgehog.framework.Screen;
import com.android.hedgehog.framework.Graphics;

public class FreePlayTripleSum extends Screen {

	final static int totalTime = 124;
	TripleSum triplesum;
	double accumulatedTime = 0.0;
	double pauseTime = 0.0;
	int Time = 1;
	int totalScore = 0;
	int phase = 0;
	boolean correct;
	boolean flag = false;
	boolean flag2 = false;

	public FreePlayTripleSum(Game game) {
		super(game);
		triplesum = new TripleSum((int) ((Math.random() * 10) + 1));

	}

	@Override
	public void update(float deltaTime) {
		accumulatedTime += deltaTime;
		if (accumulatedTime > Time)
			Time++;
		if (Time > totalTime) {
			game.setScreen(new FreePlayResult(game,1, totalScore));
		}

		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		int len = touchEvents.size();

		if (phase < 3) {
			for (int i = 0; i < len; i++) {
				TouchEvent event = touchEvents.get(i);
				if (event.type == TouchEvent.TOUCH_UP) {
					if (event.y > 352 && (event.x < 128 || event.x > 192)) {
						phase++;
						updateButtonChange(event);
						if (phase == 3) {
							flag = true;
							flag2 = true;
						}
						return;
					}
				}
			}
		} else {
			calculateScore();
			newQuestion(deltaTime);
		}

	}

	private void updateButtonChange(TouchEvent button) {

		if (button.x < 64 && button.y > 352 && button.y < 416) {// option0
			triplesum.guesses[phase - 1] = triplesum.options[0];
			if (Settings.soundEnabled)
				Assets.click.play(1);
			return;
		}
		if (button.x > 64 && button.x < 128 && button.y > 352 && button.y < 416) {// option1
			triplesum.guesses[phase - 1] = triplesum.options[1];
			if (Settings.soundEnabled)
				Assets.click.play(1);
			return;
		}
		if (button.x > 192 && button.x < 256 && button.y > 352
				&& button.y < 416) {// option2
			triplesum.guesses[phase - 1] = triplesum.options[2];
			if (Settings.soundEnabled)
				Assets.click.play(1);
			return;
		}
		if (button.x > 256 && button.y > 352 && button.y < 416) {// option3
			triplesum.guesses[phase - 1] = triplesum.options[3];
			if (Settings.soundEnabled)
				Assets.click.play(1);
			return;
		}
		if (button.x < 64 && button.y > 416) {// option4
			triplesum.guesses[phase - 1] = triplesum.options[4];
			if (Settings.soundEnabled)
				Assets.click.play(1);
			return;
		}
		if (button.x > 64 && button.x < 128 && button.y > 416) {// option5
			triplesum.guesses[phase - 1] = triplesum.options[5];
			if (Settings.soundEnabled)
				Assets.click.play(1);
			return;
		}
		if (button.x > 192 && button.x < 256 && button.y > 416) {// option6
			triplesum.guesses[phase - 1] = triplesum.options[6];
			if (Settings.soundEnabled)
				Assets.click.play(1);
			return;
		}
		if (button.x > 256 && button.y > 416) {// option7
			triplesum.guesses[phase - 1] = triplesum.options[7];
			if (Settings.soundEnabled)
				Assets.click.play(1);
			return;
		}

	}

	private synchronized void calculateScore() {
		if (flag == false)
			return;

		correct = triplesum.checkAnswer();

		if (correct)
			totalScore += (10 + triplesum.difficulty);
		else
			totalScore -= 10;

		flag = false;

	}

	private synchronized void newQuestion(float delta) {
		pauseTime += delta;

		if (pauseTime > .8 && flag2) {
			triplesum = new TripleSum((int) ((Math.random() * 10) + 1));
			phase = 0;
			pauseTime = 0.0;
			flag2 = false;
			if (Settings.soundEnabled)
				Assets.eat.play(1);
		}

	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.blackbackground, 0, 0);
		g.drawPixmap(Assets.blackbutton, 0, 416, 0, 0, 64, 64);
		g.drawPixmap(Assets.blackbutton, 64, 416, 0, 0, 64, 64);
		g.drawPixmap(Assets.blackbutton, 0, 352, 0, 0, 64, 64);
		g.drawPixmap(Assets.blackbutton, 64, 352, 0, 0, 64, 64);
		g.drawPixmap(Assets.blackbutton, 192, 416, 0, 0, 64, 64);
		g.drawPixmap(Assets.blackbutton, 256, 416, 0, 0, 64, 64);
		g.drawPixmap(Assets.blackbutton, 192, 352, 0, 0, 64, 64);
		g.drawPixmap(Assets.blackbutton, 256, 352, 0, 0, 64, 64);
		displayText();

	}

	private void displayText() {

		Graphics g = game.getGraphics();

		g.drawText("" + (totalTime - Time), 280, 20, 20, Color.WHITE); // display
																		// Time
		g.drawText("" + totalScore, 20, 20, 20, Color.WHITE); // display Score

		g.drawText("" + triplesum.options[0], 0 + 32, 384 + 16, 24, Color.WHITE);
		g.drawText("" + triplesum.options[1], 64 + 32, 384 + 16, 24,
				Color.WHITE);
		g.drawText("" + triplesum.options[2], 192 + 32, 384 + 16, 24,
				Color.WHITE);
		g.drawText("" + triplesum.options[3], 256 + 32, 384 + 16, 24,
				Color.WHITE);
		g.drawText("" + triplesum.options[4], 0 + 32, 448 + 16, 24, Color.WHITE);
		g.drawText("" + triplesum.options[5], 64 + 32, 448 + 16, 24,
				Color.WHITE);
		g.drawText("" + triplesum.options[6], 192 + 32, 448 + 16, 24,
				Color.WHITE);
		g.drawText("" + triplesum.options[7], 256 + 32, 448 + 16, 24,
				Color.WHITE);

		if (phase < 1) // 1st operand
			g.drawText("?", 20, 220, 26, Color.WHITE);
		else
			g.drawText("" + triplesum.guesses[0], 20, 220, 26, Color.WHITE);

		if (triplesum.difficulty < 6) // 1st operator
			g.drawText("+", 50, 220, 26, Color.WHITE);
		else
			g.drawText("x", 50, 220, 26, Color.WHITE);

		if (phase < 2) // 2nd operand
			g.drawText("?", 80, 220, 26, Color.WHITE);
		else
			g.drawText("" + triplesum.guesses[1], 80, 220, 26, Color.WHITE);

		g.drawText("+", 110, 220, 26, Color.WHITE);// 2nd operator

		if (phase < 3) // 3rd operand
			g.drawText("?", 140, 220, 26, Color.WHITE);
		else
			g.drawText("" + triplesum.guesses[2], 140, 220, 26, Color.WHITE);

		g.drawText("=", 170, 220, 32, Color.WHITE); // =
		g.drawText("" + triplesum.answer, 200, 220, 26, Color.WHITE); // answer

		if (phase == 3 && flag2)
			if (triplesum.answer == triplesum.totalGuess)
				g.drawText("You Scored: " + (10 + triplesum.difficulty)
						+ " points!", 150, 140, 18, Color.GREEN);
			else
				g.drawText("You Lost 10 points", 150, 140, 16, Color.RED);

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
